﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Data;


namespace MvcApplicationYoung.WcfStandaloneWPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DataTable dt;
        ServiceHost host = null;

        public MainWindow()
        {
            InitializeComponent();
            dt = CreateGridTable();
            this.dataGrid1.ItemsSource = dt.AsDataView();
        }

        private DataTable CreateGridTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("名称");
            dt.Columns.Add("类型");
            dt.Columns.Add("参数");
            dt.Columns.Add("返回");
            return dt;
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            //http 绑定 
            Type serviceType = typeof(MvcApplicationYoung.WcfServiceLibrary.Service1);
            Uri httpAddress = new Uri("http://localhost:8888/YoungMVC/Service.svc/");
            host = new ServiceHost(serviceType, new Uri[] { httpAddress });
            WSHttpBinding basicHttpBinding = new WSHttpBinding();
            string address = "test";
            //http://localhost:8888/YoungMVC/Service.svc/test
            ServiceEndpoint endPoint = host.AddServiceEndpoint(typeof(MvcApplicationYoung.WcfServiceLibrary.IService1), basicHttpBinding, address);

#if DEBUG
            foreach (OperationDescription od in endPoint.Contract.Operations)
            {

                MvcApplicationYoung.WcfServiceLibrary.OwnBehavior1 bh = new MvcApplicationYoung.WcfServiceLibrary.OwnBehavior1();
                bh.NInspector.evtBeforeCall += new WcfServiceLibrary.NewInspector.DelBeforeCall(BeforeCall);
                od.Behaviors.Add(bh);
            }
#endif
            host.Open();
            this.labelInfo.Content = "服务已启动";
            this.startButton.IsEnabled = false;
            this.stopButton.IsEnabled = true;
        }

        private void BeforeCall(string operationName, object[] inputs)
        {
            DataRow newR = dt.NewRow();
            newR[0] = operationName;
            newR[1] = "BeforeCall";
            string inputString = string.Empty;
            foreach (object ob in inputs)
            {
                if (ob != null)
                { inputString = inputString + ":" + ob.ToString() + "  "; }
            }
            newR[2] = inputString;
            newR[3] = "";
            dt.Rows.Add(newR);
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            if ((host != null) && (host.State != CommunicationState.Closed))//判断服务是否关闭
            {
                host.Close();//关闭服务
                 this.labelInfo.Content = "服务已关闭";
                 this.startButton.IsEnabled = true;
                 this.stopButton.IsEnabled = false;
            }
        }
    }
}
